package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.entity.SlideShow;
import cn.kgc.ssm.mapper.SlideMapper;
import cn.kgc.ssm.service.SlideService;
import cn.kgc.ssm.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/3/7
 * @description
 */
@Service

public class SlideServiceImpl implements SlideService {

    @Resource
    private SlideMapper slideMapper;


    @Override
    public ResponseResult slideShowPage(Integer page, Integer limit) {

        PageHelper.startPage(page,limit);

        List<SlideShow> slideShows =  slideMapper.getList();

        long total = new PageInfo<SlideShow>(slideShows).getTotal();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(slideShows);
        responseResult.setCount(total);
        responseResult.setMsg("查询成功");

        return responseResult;
    }

    @Override
    public ResponseResult addUploadInfo(String title, String uuid) {

        slideMapper.addUploadInfo(title,uuid);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("图片上传成功");
        return responseResult;
    }

    @Override
    public ResponseResult editSlide(Integer id) {

        slideMapper.updateSlide(id,new Date());

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("置顶成功");
        return responseResult;
    }

    @Override
    public ResponseResult deleteSlide(String uuid) {

        // 1. 删除本地服务器中的图片
        File file = new File("D:/upload/slides/" + uuid);
        file.delete();

        // 2.删除数据库中图片的信息
        slideMapper.deleteSlide(uuid);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");
        return responseResult;
    }

    @Override
    public ResponseResult slideList() {
         List<SlideShow> slideShows =  slideMapper.slideList();
         ResponseResult responseResult = new ResponseResult();
         responseResult.setCode(0);
         responseResult.setData(slideShows);
        return responseResult;
    }
}
